project(embeddedRTPS-Desktop)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-O0 -g -Wall -Wextra -Wnon-virtual-dtor -Wpedantic -fno-non-call-exceptions -fno-exceptions -fPIC -fno-rtti")
set(CMAKE_C_FLAGS "-O0 -g -Wall -Wextra -Wpedantic -fno-non-call-exceptions -fno-exceptions -fPIC")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

# Add micro-CDR
add_subdirectory(embeddedrtps/thirdparty/Micro-CDR)

# Add lwIP
SET(LWIP_CONTRIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/contrib")
SET(LWIP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lwip")

SET(PORT_ROOT "${LWIP_CONTRIB_DIR}/ports/unix")
SET(ARCH_DIR "${PORT_ROOT}/port/include")

include(${LWIP_CONTRIB_DIR}/ports/CMakeCommon.cmake)

set (LWIP_DEFINITIONS -DLWIP_DEBUG)
set (LWIP_INCLUDE_DIRS
        "${LWIP_DIR}/src/include"
        "${LWIP_CONTRIB_DIR}"
        "${ARCH_DIR}"
        "${CMAKE_CURRENT_SOURCE_DIR}/lwip"
        "${WPDPACK_INCLUDE_DIR}"
        )

include(${LWIP_DIR}/src/Filelists.cmake)
include(${LWIP_CONTRIB_DIR}/Filelists.cmake)
include(${PORT_ROOT}/Filelists.cmake)

#add_library(lwip STATIC ${lwipcore_SRCS} ${lwipcore3_SRCS} ${lwipcontribportunix_SRCS})
add_library(lwip SHARED ${lwipcore_SRCS} ${lwipcore3_SRCS} ${lwipcontribportunix_SRCS})

target_include_directories(lwip PUBLIC ${LWIP_INCLUDE_DIRS})
include_directories(lwip PUBLIC embeddedrtps/thirdparty/lwip/)

target_compile_options(lwip PRIVATE ${LWIP_COMPILER_FLAGS})
target_compile_definitions(lwip PRIVATE ${LWIP_DEFINITIONS})


target_link_libraries(lwip ${LWIP_SANITIZER_LIBS}  lwipcontribaddons lwipallapps lwipcore lwipcontribportunix)

# embeddedRTPS
add_library(embeddedrtps "")
include_directories(include)
include_directories(include/lwip)
include_directories(embeddedrtps/include)
include_directories(${microcdr_SOURCE_DIR}/include)
target_include_directories(embeddedrtps PUBLIC embeddedrtps/include include ${LWIP_DIR}/src/include ${microcdr_SOURCE_DIR}/include)

target_sources(embeddedrtps
        PRIVATE
        "embeddedrtps/src/rtps.c"
        "embeddedrtps/src/ThreadPool.c"
        "embeddedrtps/src/communication/UdpDriver.c"
        "embeddedrtps/src/discovery/ParticipantProxyData.c"
        "embeddedrtps/src/discovery/SPDPAgent.c"
        "embeddedrtps/src/discovery/SEDPAgent.c"
        "embeddedrtps/src/discovery/TopicData.c"
        "embeddedrtps/src/entities/Domain.c"
        "embeddedrtps/src/entities/Participant.c"
        "embeddedrtps/src/entities/StatelessReader.c"
        "embeddedrtps/src/entities/StatefulReader.c"
        "embeddedrtps/src/entities/Reader.c"
        "embeddedrtps/src/entities/Writer.c"
        "embeddedrtps/src/entities/WriterProxy.c"
        "embeddedrtps/src/entities/StatelessWriter.c"
        "embeddedrtps/src/entities/StatefulWriter.c"
        "embeddedrtps/src/messages/MessageReceiver.c"
        "embeddedrtps/src/messages/MessageTypes.c"
        "embeddedrtps/src/messages/MessageFactory.c"
        "embeddedrtps/src/storages/PBufWrapper.c"
        "embeddedrtps/src/storages/CacheChange.c"
        "embeddedrtps/src/storages/SimpleHistoryCache.c"
        "embeddedrtps/src/storages/MemoryPool.c"
        "embeddedrtps/src/storages/ThreadSafeCircularBuffer.c"
)

target_include_directories(embeddedrtps PUBLIC include ${microcdr_SOURCE_DIR}/include)
target_link_libraries(embeddedrtps PRIVATE lwip microcdr)

# embeddedRTPS test
add_subdirectory(tests)

# FastDDS
add_subdirectory(fastdds)
